package com.kylin.upms.biz.web;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.kylin.upms.biz.dto.RoleDto;
import com.kylin.upms.biz.entity.Role;
import com.kylin.upms.biz.entity.User;
import com.kylin.upms.biz.service.IRoleService;
import com.kylin.upms.biz.vo.ResEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mht
 * @since 2019-09-15
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    IRoleService iRoleService;

    @RequestMapping(value = "/page",method = RequestMethod.GET)
    public ResEntity rolePage(){

        return ResEntity.ok();
    }

    @CrossOrigin(origins = {"http://localhost:8081"})
    @RequestMapping(value = "/roleSele",method = RequestMethod.GET)
    public ResEntity roleSele(){
        EntityWrapper entityWrapper = new EntityWrapper();
        List list = iRoleService.selectList(entityWrapper);
        return ResEntity.ok("查询成功",list);
    }
    @CrossOrigin(origins = {"http://localhost:8081"})
    @RequestMapping(value = "/roleSeleByUser",method = RequestMethod.GET)
    public ResEntity roleSeleByUser(Integer id){
        List list = iRoleService.roleSeleByUser(id);
        return ResEntity.ok("查询成功",list);
    }
    @CrossOrigin(origins = {"http://localhost:8081"})
    @RequestMapping(value = "/updRoleByUser",method = RequestMethod.POST)
    public ResEntity updRoleByUser(Integer uid,Integer[] rids){
        for (Integer rid : rids) {
            Integer insert = iRoleService.addUserByRole(uid,rid);
        }
        return ResEntity.ok("插入成功");
    }
    @CrossOrigin(origins = {"http://localhost:8081"})
    @RequestMapping(value = "/rolePage",method = RequestMethod.GET)
    public ResEntity rolePage(RoleDto roleDto){
        Page<Role> page = new Page<>(roleDto.getPageNum(),roleDto.getPageSize());
        Role role = new Role();
        BeanUtils.copyProperties(roleDto,role);
        EntityWrapper entityWrapper = new EntityWrapper(role);
        Page page1= iRoleService.selectPage(page,entityWrapper);
        return ResEntity.ok("查询成功",page1);
    }
    @CrossOrigin(origins = {"http://localhost:8081"})
    @RequestMapping(value="/roleAdd",method = RequestMethod.GET)
    public ResEntity roleAdd(RoleDto roleDto){
        Role role = new Role();
        BeanUtils.copyProperties(roleDto,role);
        boolean insert = iRoleService.insert(role);
        return ResEntity.ok("插入成功");
    }
    @CrossOrigin(origins = {"http://localhost:8081"})
    @RequestMapping(value="/roleById",method = RequestMethod.GET)
    public ResEntity roleById(Integer id){

        Role role = iRoleService.selectById(id);
        return ResEntity.ok("查询成功",role);
    }
    @CrossOrigin(origins = {"http://localhost:8081"})
    @RequestMapping(method = RequestMethod.GET,value = "/update")
    public ResEntity update(RoleDto roleDto){
        Role role = new Role();
        BeanUtils.copyProperties(roleDto,role);

        System.out.println(role);
        boolean update = iRoleService.updateById(role);
        if(update){
            return ResEntity.ok("审核成功");
        }
        return ResEntity.error("审核失败");
    }
}
